package com.tsingteng.cosfun.ui.focus;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.util.Pair;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.tsingteng.cosfun.MainActivity;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseFragment;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverListener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverManager;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.NotchUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 关注 fragment
 * 创建人: 肖利
 * 时间: 2018/7/30
 * qq: (872179966)
 */

public class FocusFragment extends BaseFragment implements ObserverListener {
    String tag = FocusFragment.class.getSimpleName();
    public ViewPager viewpager = null;
    MyFragmentStatePagerAdapter adapter = null;
    public List<Pair<String, Fragment>> mFragmentPair = null;
    int currentPosition = 0;
    @Override
    protected void initView() {
        viewpager = rootView.findViewById(R.id.focus_viewpager);
        mFragmentPair = new ArrayList<Pair<String, Fragment>>();

//        Pair awkwardPair = new Pair("focus_awkward", new AwkwardFragment());
        Pair opusPair = new Pair("focus_opus", new FocusOpusFragmentNew());
        HomeMineFragment homeMineFragment = new HomeMineFragment();
        homeMineFragment.setCurrentPosition(1);
        homeMineFragment.setBackLisenter(backLisenter);
        Pair personalPair = new Pair("focus_personal", homeMineFragment);
//        mFragmentPair.add(awkwardPair);
        mFragmentPair.add(opusPair);
        mFragmentPair.add(personalPair);

        adapter = new MyFragmentStatePagerAdapter(getFragmentManager(), mFragmentPair);
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(currentPosition);
        viewpager.setOffscreenPageLimit(2);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                switch (currentPosition) {
                    case 0:
                        ((MainActivity) getActivity()).setVibButtomView(View.VISIBLE);
//                        if (mFragmentPair.get(0).first.equals("focus_awkward")) {
//                            ((AwkwardFragment) (mFragmentPair.get(0).second)).getData();
//                            ((AwkwardFragment) (mFragmentPair.get(0).second)).playVideo(-1);
//                        }

                        if (mFragmentPair.get(0).first.equals("focus_opus")) {
                            ((FocusOpusFragmentNew) (mFragmentPair.get(0).second)).play();
                        }

                        break;
                    case 1:
//                        if (mFragmentPair.get(0).first.equals("focus_awkward")) {
//                            ((AwkwardFragment) (mFragmentPair.get(0).second)).onPause();
//                        }

                        if (mFragmentPair.get(0).first.equals("focus_opus")) {
                            ((FocusOpusFragmentNew) (mFragmentPair.get(0).second)).onPause();
                        }
                        if (mFragmentPair.get(1).first.equals("focus_personal")) {
                            ((HomeMineFragment) (mFragmentPair.get(1).second)).refreshData();
                            ((HomeMineFragment) (mFragmentPair.get(1).second)).goneButtomView(View.GONE);
                        }
                        ((MainActivity) getActivity()).setVibButtomView(View.GONE);
                        break;
                    case 2:
                        if (mFragmentPair.get(1).first.equals("focus_opus")) {
                            ((FocusOpusFragmentNew) (mFragmentPair.get(1).second)).onPause();
                        }
                        ((MainActivity) getActivity()).setVibButtomView(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        ObserverManager.getInstance().add(this);
    }

    @Override
    public void onResume() {
        super.onResume();
//        Log.e("test23", tag + "   onResume---->playVideo");
//        playVideo();
    }

    public void play() {
        playVideo();
    }

    @Override
    public void onPause() {
        super.onPause();
        stopVideo();
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    private void playVideo() {
        if (mFragmentPair == null || mFragmentPair.size() == 0 ||
                mFragmentPair.size() < currentPosition) {
            return;
        }
        if (mFragmentPair.get(currentPosition).first.equals("focus_opus")) {
            ((FocusOpusFragmentNew) (mFragmentPair.get(currentPosition).second)).onRefreshData();
        }
//        if (mFragmentPair.get(currentPosition).first.equals("focus_awkward")) {
//            ((AwkwardFragment) (mFragmentPair.get(currentPosition).second)).playVideo(-1);
//        }

    }

    private void stopVideo() {
        if (mFragmentPair == null || mFragmentPair.size() == 0 ||
                mFragmentPair.size() < currentPosition) {
            return;
        }
        if (mFragmentPair.get(0).first.equals("focus_opus")) {
            ((FocusOpusFragmentNew) (mFragmentPair.get(0).second)).onPause();
        }
//        if (mFragmentPair.get(currentPosition).first.equals("focus_awkward")) {
//            ((AwkwardFragment) (mFragmentPair.get(currentPosition).second)).onPause();
//        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_focus;
    }

    @Override
    public void observerUpData(int eventType, int index, int id) {
        switch (eventType) {
            case ObserverListener.EVENT_AWKWARD:
                viewpager.setCurrentItem(0);
                break;
        }
    }

    public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

        private List<Pair<String, Fragment>> mFragmentPair;

        public MyFragmentStatePagerAdapter(FragmentManager fm, List<Pair<String, Fragment>> mFragmentPair) {
            super(fm);
            this.mFragmentPair = mFragmentPair;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentPair.get(position).second;
        }

        /**
         * adapter.notifyDataSetChanged()的时候，ViewPager会去查询到底哪个位置有改变，
         * return PagerAdapter.POSITION_NONE; 就是告诉所有的View已经变化，从而达到刷新的目的
         *
         * @param object
         * @return
         */
        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
            //  return super.getItemPosition(object);
        }

        @Override
        public int getCount() {
            return mFragmentPair.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentPair.get(position).first;
        }

        @Override
        public void registerDataSetObserver(@NonNull DataSetObserver observer) {
        }
    }

    public void shark() {
        LogUtils.e("shark", "FcousFragment");
        if (mFragmentPair.get(currentPosition).first.equals("focus_opus")) {
            ((FocusOpusFragmentNew) (mFragmentPair.get(currentPosition).second)).shark();
        }
    }

    HomeMineFragment.BackLisenter backLisenter = new HomeMineFragment.BackLisenter() {
        @Override
        public void back() {
            viewpager.setCurrentItem(0);
        }
    };
}
